﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GTAuto.DAL.DAL;
using GTAuto.DAL.Entities;

namespace GTauto.Site.FrontEnd.AppCode.HtmlHelpers
{
    /// <summary>
    /// Хелпер для отображание хлебных крошек 
    /// </summary>
    public static class BreadCrumbsHelper
    {
        public static string BreadCrumb(this HtmlHelper html, string url)
        {


            string level1 = "<ul><li class=\"active\"><a href=\"#\">{0}</a></li></ul>";


            string level2 =
                "<ul><li><a href=\"{0}\">Каталог авто</a></li><li class=\"active\"><a href=\"#\">{1}</a></li></ul>";

            string level22 =
               "<ul><li><a href=\"{0}\">{1}</a></li><li class=\"active\"><a href=\"#\">{2}</a></li></ul>";

            //string level3 =
            //    "<ul><li><a href=\"{0}\">Каталог авто</a></li><li><a href=\"{1}\">{2}</a></li><li class=\"active\"><a href=\"#\">{3}</a></li></ul>";

            string level3 =
               "<ul><li><a href=\"{0}\">{1}</a></li><li><a href=\"{2}\">{3}</a></li><li class=\"active\"><a href=\"#\">{4}</a></li></ul>";


            // "<ul><li><a href=\"#\">Шины и диски</a></li><li><a href=\"#\">Производители</a></li><li><a href=\"#\">Rosava</a></li><li class=\"active\"><a href=\"#\">AS-701</a></li></ul>";

            string level4 =
                "<ul><li><a href=\"{0}\">{1}</a></li><li><a href=\"{2}\">{3}</a></li><li><a href=\"{4}\">{5}</a></li><li class=\"active\"><a href=\"#\">{6}</a></li></ul>";



            string retVal = "";

            url = url.ToLower();
            

            if (url.Contains("firms/firmview"))
            {
                if (HttpContext.Current.Session["CurFirmCategory"] != null)
                {

                    int catId = (int) HttpContext.Current.Session["CurFirmCategory"]; 
                    GTFirmCategory gtFirmCategory = DALFirmCategory.Singleton.Get(catId);
                    var modelId = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last();
                    GTFirm gtFirm = DALFirms.Singleton.Get(Convert.ToInt32(modelId));
                    retVal = string.Format(level3, @"\Firms\Index", "Каталог фирм", @"\Firms\FirmsByCat\" + catId, gtFirmCategory.Name,
                                           gtFirm.Name);
                }
            }

            if (url.Contains("firms/firmsbycat"))
            {
                var modelId = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last();
                var cat = DALFirmCategory.Singleton.Get(Convert.ToInt32(modelId));
                retVal = string.Format(level22, @"\Firms\Index", "Каталог фирм", cat.Name);
            }


            if (url.Contains("tires/tirebymodel") || url.Contains("discs/discbymodel"))
            {
                var modelId = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last();
                var model = DALModels.Singleton.Get(Convert.ToInt32(modelId));
                retVal = string.Format(level4, @"\Tires\Index", "Шины и диски", @"\Tires\Index", "Производители", @"\Tires\TireByBrand\" + model.BrandId.ToString(), model.Brand.Name, model.Name);
            }




            if (url.Contains("tires/tiresearchresult"))
            {

                retVal = string.Format(level3, @"\Tires\Index", "Шины и диски", @"\Tires\Index", "Производители", "Результаты поиска шин");
            }


            if (url.Contains("tires/tirebybrand") || url.Contains("discs/discbybrand"))
            {
                var brandId = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last();
                var brand = DALBrands.Singleton.Get(Convert.ToInt32(brandId));
                retVal = string.Format(level3, @"\Tires\Index", "Шины и диски", @"\Tires\Index", "Производители", brand.Name);
            }


            if (url.Contains("tires/index"))
            {
                retVal = string.Format(level1, "Шины и диски");
            }

            if (url.Contains("autobrands"))
            {
                var brandId = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last();
                var brand = DALBrands.Singleton.Get(Convert.ToInt32(brandId));
                retVal = string.Format(level2, @"\Home\Index", brand.Name);
            }


            if (url.Contains("automodel"))
            {
                var modelId = HttpContext.Current.Request.Url.AbsolutePath.Split('/').Last();
                var model = DALModels.Singleton.Get(Convert.ToInt32(modelId));

                retVal = string.Format(level3, @"\Home\Index", "Каталог авто", @"\AutoBrands\Index\" + model.BrandId, model.Brand.Name, model.NameDate);
            }





            return retVal;
        }
    }

}